package com.xuecheng.teaching.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.WorkChaDto;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.mapper.WorkMapper;
import com.xuecheng.teaching.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作业 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-14
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {
    @Override
    public ResponseResult findByPage(PageRequest params, WorkChaDto dto) {
        // 1.构建分页
        Page<Work> page = new Page<>(params.getPageNo(), params.getPageSize());
        //2.构建条件
        LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
        //2.1 id
        qw.eq(Work::getCompanyId, AuthInfoHolder.getCompanyId());
        //2.2 根据标题模糊搜索
        if (ObjectUtil.isNotEmpty(dto)) {
            qw.like(StrUtil.isNotBlank(dto.getTitle()), Work::getTitle, dto.getTitle());
        }
        //分页查寻
        page = this.page(page, qw);

        // qw.like(Work::getTitle,dto.getTitle() );


        //取出数据
        List<Work> workList = page.getRecords();

        ArrayList<WorkVo> list = new ArrayList<>();
        for (Work work : workList) {
            WorkVo workVo = new WorkVo();
            if (work.getChangeDate() == null) {//如果修改时间为空 设置创建时间为初始时间
                workVo.setChangeDate(work.getCreateDate());
            } else {
                workVo.setChangeDate(work.getChangeDate());//设置修改时间
            }
            workVo.setQuestion(work.getQuestion());//内容
            workVo.setStatus(work.getStatus());//状态
            workVo.setWorkId(work.getId());//作业id
            workVo.setTitle(work.getTitle());//标题
            workVo.setCourseName(work.getCourseName());

            list.add(workVo);
        }

     //返回
        return PageResponseResult.okResult(page.getTotal(), list);
    }

    //新增修改作业
    @Override
    public ResponseResult savework(WorkDto dto) {
        // 1.校验参数合法性
        if (dto.getTitle() == null || (dto.getQuestion() == null)) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //2转实体类
        Work work = BeanHelper.copyProperties(dto, Work.class);
        if (dto.getWorkId() != null) {
            work.setChangeDate(DateUtil.date());// 修改时间
            work.setId(dto.getWorkId());
            work.setCourseName(dto.getCourseName());
            work.setStatus(dto.getStatus());


            //更新数据库
            this.updateById(work);
        } else {
            //创建时间
            work.setCreateDate(DateUtil.date());
            //使用状态
            work.setStatus(CommonConstants.DELETE_FLAG);
            //创建人
            work.setUsername(AuthInfoHolder.getUserId().toString());
            //新增数据库
            this.save(work);
        }


        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult deletework(Long workId) {
        // 1.先查作业
        Work work = this.getById(workId);
        //
        if (work.getStatus().equals(CommonConstants.USING_FLAG)) {
            throw new BusinessException(ErrorCode.ERROR);
        }
        //删除数据库
        this.removeById(workId);


        // 5.返回结果
        return ResponseResult.okResult();
    }

}
